package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarParamsMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarParams;
import com.wanghaixin.pojo.CarParamsExample;
import com.wanghaixin.service.CarParamTypeService;
import com.wanghaixin.service.CarParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 10:38
 * @Description:
 */
@Service
public class CarParamsServiceImpl implements CarParamsService {

    @Autowired
    private CarParamsMapper carParamsMapper;



    @Override
    public int addCarParams(CarParams carParams) {
        int i = carParamsMapper.insertSelective(carParams);
        return i;
    }

    @Override
    public int delCarParams(Integer id) {
        int i = carParamsMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int upCarParams(CarParams carParams) {
        int update = carParamsMapper.updateByPrimaryKeySelective(carParams);
        return update;
    }

    @Override
    public CarParams findById(Integer id) {
        CarParams carParams = carParamsMapper.selectByPrimaryKey(id);
        return carParams;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());

        CarParamsExample example = new CarParamsExample();
        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause("id");
        }

        List<CarParams> carParams = carParamsMapper.selectByExample(example);

        PageInfo info = new PageInfo(carParams);
        long total = info.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, carParams);
        return dataGridResult;
    }

    @Override
    public List<CarParams> findAll() {
        List<CarParams> carParams = carParamsMapper.selectByExample(null);
        return carParams;
    }
}
